package com.hunlian.makelove.net;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 网络状态判断
 * @author jiangsb
 * 2014-06-06
 *
 */
public class TNetworkState {
	//检查网络是否处于连接状态
	public static boolean isNetworkConnected(Context context) {
		if (context != null) {
			ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
			if(connectivityManager != null){
				NetworkInfo mNetworkInfo = connectivityManager.getActiveNetworkInfo();
				if (mNetworkInfo != null) {
					return mNetworkInfo.isAvailable();
				}
			}
		}
		return false;
	}
	
	//监测网络状态是否处于连接状态，功能基本同isNetworkConnected
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}
	
	//判断wifi网络是否可用
	public static boolean isWifiConnected(Context context) {
		if (context != null) {
			//WifiManager wManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
			ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
			if(connectivityManager != null){
				NetworkInfo mWiFiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
				if (mWiFiNetworkInfo != null) {
					return mWiFiNetworkInfo.isAvailable();
				}
			}
		}
		return false;
	}
	
	//判断mobile网络是否可用
	public static boolean isMobileConnected(Context context) {
		if (context != null) {
			ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
			if(connectivityManager != null){
				NetworkInfo mMobileNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
				if (mMobileNetworkInfo != null) {
					return mMobileNetworkInfo.isAvailable();
				}
			}
		}
		return false;
	}
	
	//获取当前网络连接的类型信息 
	public static int getConnectedType(Context context) {
		if (context != null) {
			ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
			if(connectivityManager != null){
				NetworkInfo mNetworkInfo = connectivityManager.getActiveNetworkInfo();
				if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
					return mNetworkInfo.getType();
				}
			}
		}
		return -1;
	}
	public static String getConnectedTypeName(Context context) {
		if (context != null) {
			ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
			if(connectivityManager != null){
				NetworkInfo mNetworkInfo = connectivityManager.getActiveNetworkInfo();
				if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
					return mNetworkInfo.getTypeName();
				}
			}
		}
		return "";
	}
	
	/** 
	* @author sky 
	* Email vipa1888@163.com 
	* QQ:840950105 
	* 获取当前的网络状态 -1：没有网络 1：WIFI网络2：wap网络3：net网络 ,0:其他网络
	* cmwap和cmnet是GPRS网络的两种接入方式。其实上吧，是wap方式和net方式。cm是chinamobile
	* @param context 
	* @return 
	* modify by jiangsb 2014-06-06 增加0其他网络的返回，修改nType判断为switch判断，不用if-else
	*/ 
	public static int getAPNType(Context context) {
		int netType = -1;
		ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if(connMgr != null){
			NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
			if (networkInfo == null) {
				return netType;
			}
			
			int nType = networkInfo.getType();

			switch (nType) {
			case ConnectivityManager.TYPE_WIFI:
				netType = 1; 
				break;
			case ConnectivityManager.TYPE_MOBILE:
				if(networkInfo.getExtraInfo().equalsIgnoreCase("cmnet")){
					netType = 3; //CMNET;
				}
				else{
					netType = 2; //CMWAP;
				}
				break;
			default:
				netType = 0;
				break;
			}
		}
		return netType;
	}
}
